package community.hospital.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.annotation.Log;
import community.hospital.annotation.Module;
import community.hospital.annotation.NeedLogin;
import community.hospital.annotation.NeedPermission;
import community.hospital.common.Add;
import community.hospital.common.R;
import community.hospital.entity.request.ListOrderRequest;
import community.hospital.entity.request.OrderRequest;
import community.hospital.entity.response.OrderDetailResp;
import community.hospital.entity.response.OrderResponse;
import community.hospital.service.application.OrderApplicationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static community.hospital.common.Constant.*;
import static community.hospital.common.PermissionConstant.LIST_ORDER;
import static community.hospital.common.PermissionConstant.SUPER_ADMIN;

/**
 * @author yindingjie
 */
@RestController
@RequestMapping("/order")
@Module("订单")
public class OrderController {

    @Resource
    private OrderApplicationService orderApplicationService;

    @NeedLogin
//    @NeedPermission(role = SUPER_ADMIN, permission = LIST_ORDER)
    @PostMapping("/listOrder")
    @Log(methodName = LOG_QUERY,desc = "查询订单")
    public R<Page<OrderResponse>> listOrder(@Valid @RequestBody ListOrderRequest request) {
        Page<OrderResponse> responsePage = orderApplicationService.listOrder(request);
        return R.ok(responsePage);
    }

    @NeedLogin
    @Log(methodName = LOG_QUERY,desc = "分页查询订单")
    @PostMapping("/listOrderByUserId")
    public R<Page<OrderResponse>> listOrderByUserId(@Valid @RequestBody ListOrderRequest request) {
        ListOrderRequest listOrderRequest = new ListOrderRequest();
        listOrderRequest.setUserId(request.getUserId());
        listOrderRequest.setPageNumber(request.getPageNumber());
        listOrderRequest.setPageSize(request.getPageSize());
        Page<OrderResponse> orderResponsePage = orderApplicationService.listOrder(listOrderRequest);
        return R.ok(orderResponsePage);
    }

    @NeedLogin
    @PostMapping("/addOrder")
    @Log(methodName = LOG_INSERT,desc = "新增订单")
    public R<Boolean> addOrder(@Validated(Add.class) @RequestBody OrderRequest request) {
        Boolean isOk = orderApplicationService.addOrder(request);
        return R.ok(isOk);
    }

    @NeedLogin
    @PostMapping("/updateOrderState/{orderId}/{state}")
    @Log(methodName = LOG_UPDATE,desc = "修改订单状态")
    public R<Boolean> updateOrderState(@PathVariable("orderId") Long orderId, @PathVariable("state") Integer state) {
        Boolean isOk = orderApplicationService.updateOrderState(orderId, state);
        return R.ok(isOk);
    }

    @NeedLogin
    @GetMapping("/getCurrentUserOrder")
    @Log(methodName = LOG_QUERY,desc = "查询当前用户订单")
    public R<IPage<OrderDetailResp>> getUserOrder() {
        IPage<OrderDetailResp> currentUserOrder = orderApplicationService.getCurrentUserOrder();
        return R.ok(currentUserOrder);
    }

    @NeedLogin
    @GetMapping("/getRecentlyOrder")
    public R<OrderDetailResp> getRecentlyOrder() {
        OrderDetailResp orderDetailResp = orderApplicationService.getRecentlyOrder();
        return R.ok(orderDetailResp);
    }

}
